package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.net.RequestParam;
import com.shaoshaohuo.app.utils.Okhttputils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/7/12.
 */

public class MapZootm extends BaseActivity {

    private MapView mMapView;
    private com.amap.api.maps.AMap aMap;
    private String orderid;
    private long time = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maozootm);
        Intent intent = getIntent();
        orderid = intent.getStringExtra("orderid");
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState);

        if (aMap == null) {
            aMap = mMapView.getMap();
        }
//        LatLng latLng = new LatLng(39.906901,116.397972);
//        MarkerOptions markerOption = new MarkerOptions();
//        markerOption.position(Constants.XIAN);
////        markerOption.title("西安市").snippet("西安市：34.341568, 108.940174");
////        markerOption.draggable(true);//设置Marker可拖动
//        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
//                .decodeResource(getResources(),R.mipmap.wuliuche)));
//        // 将Marker设置为贴地显示，可以双指下拉地图查看效果
//        markerOption.position(latLng);
//        aMap.addMarker(markerOption);
//       aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(39.906901,116.397972),18,30,0)));
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        network();
                    }
                });
            }
        }, 0, 1000);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    public void network() {
//        ：/orderhandle/userll
        long l = System.currentTimeMillis();
        if (time == 0 || l - time >= 1000) {
            Map<String, String> headerParams = RequestParam.getHeaderParams();
            HashMap<String, String> stringStringHashMap = new HashMap<>();
            stringStringHashMap.put("orderid", orderid);
            Okhttputils.Instanse(this).postFromBody(headerParams, HttpConfig.BASE_HOST_URL + "/orderhandle/userll", stringStringHashMap, new Okhttputils.OkutListener() {
                @Override
                public void error(Call call, IOException e) {

                }

                @Override
                public void error(Call call, int code) {

                }

                @Override
                public void suseff(Call call, Response response, Object o) {

                    MapEntity mapEntity = (MapEntity) o;
                    final String latitude = mapEntity.getData().getLatitude();
                    final String longitude = mapEntity.getData().getLongitude();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            aMap.clear();
                            LatLng latLng = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));
                            MarkerOptions markerOption = new MarkerOptions();
                            //Constants.XIAN

//        markerOption.title("西安市").snippet("西安市：34.341568, 108.940174");
//        markerOption.draggable(true);//设置Marker可拖动
                            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                                    .decodeResource(getResources(), R.mipmap.wuliuche)));
                            // 将Marker设置为贴地显示，可以双指下拉地图查看效果
                            markerOption.position(latLng);
                            aMap.addMarker(markerOption);
                            aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, 18, 30, 0)));
                        }
                    });
                }
            }, MapEntity.class);
        }

    }
}

